package com.example.lastwork;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity2 extends AppCompatActivity {
    private RadioGroup rg_main;
    private String id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        rg_main=findViewById(R.id.rg_main);

        FragmentManager fragmentManager = getSupportFragmentManager();//开启事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.fragment ,new FristFragment().getFristFragment() );
        transaction.commit();
        rg_main.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                for (int i = 0; i < rg_main.getChildCount(); i++) {
                    RadioButton rb = (RadioButton) group.getChildAt(i);
                    if (rb.isChecked()) {
                        setIndexSelected(i);
                        break;
                    }
                }
            }
        });
    }




    private void changeFragment(Fragment fragment)
    {
        Bundle bundle = new Bundle();
        bundle.putString("id",id);
        fragment.setArguments(bundle);
        FragmentManager fragmentManager = getSupportFragmentManager();//开启事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.fragment , fragment);
        transaction.commit();
    }

    public void OnClick(View view){
        UserDao userDao = new UserDao();
        EditText E_title = findViewById(R.id.C1);
        EditText E_content = findViewById(R.id.C2);
        String title = E_title.getText().toString();
        String content = E_content.getText().toString();
        try {
            if(id != null) {
                if(!TextUtils.isEmpty(title) && !TextUtils.isEmpty(content)) {
                    UserDao.addcon(title, content, id);
                    Intent intent= new Intent();
                    intent.setClass(MainActivity2.this,MainActivity2.class);
                    startActivity(intent);
                }else{
                    Toast.makeText(MainActivity2.this, "标题或内容不能为空", Toast.LENGTH_SHORT).show();
                }
            }else{
                Toast.makeText(MainActivity2.this, "您尚未登录，请先登录", Toast.LENGTH_SHORT).show();
                Intent intent= new Intent();
                intent.setClass(MainActivity2.this,MainActivity2.class);
                startActivity(intent);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //通过index判断当前加载哪个界面
    public void setIndexSelected(int index)
    {
        switch (index)
        {
            case 0:
                changeFragment(new FristFragment().getFristFragment());
                break;
            case 1:
                changeFragment(new SecondFragment().getSecondFragment());
                break;
            case 2:
                changeFragment(new ThirdFragment().getThirdFragment());
                break;
            case 3:
                changeFragment(new FiveFragment().getFiveFragment());
                break;
            default:
                break;
        }
    }
}